import 'package:enterprise_v6_app/app/components/dialog/custom_hazard_investigation.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/constans/hazard_investigation_code.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/task/base_danger_investigation_info_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/task/process_currency_execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/hazard_investigation/hazard_management/hazard_management_controller.dart';
import 'package:get/get.dart';

class HazardRectificationController extends GetxController {
  /// 父类Controller
  final managementController = Get.find<HazardManagementController>();

  /// 隐患id
  late String id;

  /// 是否是展开状态
  RxBool isExpanded = false.obs;

  /// 是否为隐患验收页面 ture:隐患验收；false:隐患整改
  bool isAcceptance = false;

  /// 接口上传的详情实体
  BaseDangerInvestigationInfoEntity infoEntity = BaseDangerInvestigationInfoEntity();

  /// 流程节点执行实体
  ProcessCurrencyExecuteEntity executeEntity = ProcessCurrencyExecuteEntity();

  @override
  void onInit() {
    super.onInit();
    final arguments = Get.arguments as Map<String, dynamic>;
    id = arguments['id'] as String;
    isAcceptance = arguments['isAcceptance'] as bool;
    _getLedgerDetail();
  }

  /// 查询隐患治理详情
  Future<void> _getLedgerDetail() async {
    final res = await HttpUtil().generateGetRequest<BaseDangerInvestigationInfoEntity>(Interface.getLedgerDetail, queryParameters: {'id': id}).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        infoEntity = result.data!;
        update();
      }
    });
  }

  /// 照片
  List<String> picList = [
    '/2023-09-27/1695890573398signature.png',
    '/2023-09-27/1695890573398signature.png',
    '/2023-09-27/1695890573398signature.png',
  ];

  /// 视频
  List<String> videoList = [
    '/2023-10-05/16964950696387a9c6854-2442-478a-955a-55affa5eb85e523963239593223314.mp4',
    '/2023-10-05/16964950696387a9c6854-2442-478a-955a-55affa5eb85e523963239593223314.mp4',
  ];

  /// 点击展开或者收起
  void onExpanded() {
    isExpanded.value = !isExpanded.value;
  }

  /// 驳回
  void reject() {
    CustomHazardInvestigation(
      title: '驳回',
      hintText: '驳回原因',
      onConfirm: (HazardCallbackBean bean) async {
        if (bean.content.isEmpty) {
          AppToast.showWarning('请输入驳回原因!');
          return;
        }
        executeEntity.processEncoding = HazardInvestigationCode.processEncoding;
        executeEntity.nodeId = managementController.implementNode.nodeId;
        executeEntity.rtemplateId = managementController.implementNode.rtemplateId;
        infoEntity.checkAcceptComment = bean.content;
        infoEntity.checkAcceptImages = bean.pictureList;
        infoEntity.dangerState = '10';
        executeEntity.acceptDTO = infoEntity;
        // 调用流程节点执行接口
        final res = await HttpUtil().generatePostRequest(Interface.processLedgerExecute, data: executeEntity.toJson()).execute();
        res.onSuccess((result) {
          managementController.refreshPage();
          Get.back();
          AppToast.showSuccess('隐患驳回成功');
        });
        res.onError((result) {
          AppToast.showError('隐患驳回失败');
        });
      },
    ).showBottomSheetDialog();
  }

  /// 提交
  void complete() {
    // 确认弹窗
    CustomHazardInvestigation(
      title: isAcceptance ? '通过' : '隐患整改',
      hintText: isAcceptance ? '验收情况' : '整改说明',
      onConfirm: (HazardCallbackBean bean) {
        executeEntity.processEncoding = HazardInvestigationCode.processEncoding;
        executeEntity.nodeId = managementController.implementNode.nodeId;
        executeEntity.rtemplateId = managementController.implementNode.rtemplateId;
        if (isAcceptance) {
          // 验收
          acceptanceSubmit(bean);
        } else {
          // 整改
          rectificationSubmit(bean);
        }
        // Get.back();
        // AppToast.showSuccess('确认成功!');
      },
    ).showBottomSheetDialog();
  }

  /// 隐患整改提交逻辑
  Future<void> rectificationSubmit(HazardCallbackBean bean) async {
    // if (bean.content.isEmpty) {
    //   AppToast.showWarning('请输入整改描述!');
    //   return;
    // }
    // if (bean.pictureList.isEmpty) {
    //   AppToast.showWarning('请提交整改图片!');
    //   return;
    // }
    infoEntity.liableDesc = bean.content;
    infoEntity.liableImages = bean.pictureList;
    infoEntity.dangerState = '1';
    executeEntity.liableDTO = infoEntity;
    // 调用流程节点执行接口
    final res = await HttpUtil().generatePostRequest(Interface.processLedgerExecute, data: executeEntity.toJson()).execute();
    res.onSuccess((result) {
      managementController.refreshPage();
      Get.back();
      AppToast.showSuccess('隐患整改成功');
    });
    res.onError((result) {
      AppToast.showError('隐患整改失败');
    });
  }

  /// 隐患验收逻辑
  Future<void> acceptanceSubmit(HazardCallbackBean bean) async {
    // if (bean.content.isEmpty) {
    //   AppToast.showWarning('请输入验收情况!');
    //   return;
    // }
    // if (bean.pictureList.isEmpty) {
    //   AppToast.showWarning('请提交验收图片!');
    //   return;
    // }
    infoEntity.checkAcceptComment = bean.content;
    infoEntity.checkAcceptImages = bean.pictureList;
    infoEntity.dangerState = '9';
    executeEntity.acceptDTO = infoEntity;
    // 调用流程节点执行接口
    final res = await HttpUtil().generatePostRequest(Interface.processLedgerExecute, data: executeEntity.toJson()).execute();
    res.onSuccess((result) {
      managementController.refreshPage();
      Get.back();
      AppToast.showSuccess('隐患验收成功');
    });
    res.onError((result) {
      AppToast.showError('隐患验收失败');
    });
  }
}
